package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.pojo.Medicine;
import com.woniu.mapper.MedicineMapper;
import com.woniu.service.IMedicineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.DrugQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 药品表 服务实现类
 * </p>
 *
 * @author 刘岩
 * @since 2022-10-10
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements IMedicineService {

    @Autowired(required = false)
    private MedicineMapper medicineMapper;
    
    /**
     * 分页 + 模糊搜索药品
     * @param currentPage
     * @param pageSize
     * @param drugQueryVo
     * @return
     */
    @Override
    public PageResult search(Integer currentPage, Integer pageSize, DrugQueryVo drugQueryVo) {
        //创建分页对象
        IPage<Medicine> iPage = new Page<>();
        //构建查询对象
        QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
        //判断查询条件非空
        if (drugQueryVo != null) {
            if (drugQueryVo.getTitle() != null && drugQueryVo.getTitle().length() > 0) {
                wrapper.like("title", drugQueryVo.getTitle());
            }
            if(drugQueryVo.getCreateTime() != null){
                wrapper.gt("create_time", drugQueryVo.getCreateTime());
            }
            if(drugQueryVo.getUpdateTime() != null){
                wrapper.lt("create_time", drugQueryVo.getUpdateTime());
            }
        }

        //调用mapper分页查询
        IPage<Medicine> DrugIPage = medicineMapper.selectPage(iPage, wrapper);
        //封装返回对象
        return new PageResult(DrugIPage.getTotal(),DrugIPage.getRecords());
    }

    /**
     * 模糊搜索药品
     * @param title
     * @return
     */
    @Override
    public List<Medicine> queryByTitle(String title) {
        //构建查询对象
        QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
        //判断查询条件非空
        if (title != null && title.length() > 0) {
            wrapper.like("title",title);
        }
        //执行查询方法
        List<Medicine> medicineList = medicineMapper.selectList(wrapper);

        //封装返回对象
        return medicineList;
    }

    /**
     * 修改课程状态
     * @param medicineId
     * @return
     */
    @Override
    public boolean saveStatus(Long medicineId) {
        UpdateWrapper<Medicine> updateWrapper = new UpdateWrapper<>();
        Medicine medicine = medicineMapper.selectById(medicineId);
        if(!StringUtils.isEmpty(medicine.getStatus()) && medicine.getStatus().equals("0")){
            updateWrapper.set("status", "1");
            updateWrapper.eq("id", medicineId);
            return update(updateWrapper);
        }
        if(!StringUtils.isEmpty(medicine.getStatus()) && medicine.getStatus().equals("1")){
            updateWrapper.set("status", "0");
            updateWrapper.eq("id", medicineId);
            return update(updateWrapper);
        }
        return false;
    }

    /**
     * 查询轮播图排序的最大值
     * @return
     */
    @Override
    public Integer queryMaxSortByCourseId() {
        return medicineMapper.selectMaxSortByCourseId();
    }
}
